package com.xxkk.spring_boot_web.controller;

import com.alibaba.fastjson.JSONObject;
import com.xxkk.spring_boot_web.sdk.KBZ.KBZSDK;
import com.xxkk.spring_boot_web.sql.impl.KBZUserImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "/xnet")
public class XnetPayCtrl {

    private static final Logger logger = Logger.getLogger(XnetPayCtrl.class);
    @Autowired
    private KBZUserImpl kbzUserImpl;
    private final String fail = "FAIL";

    @RequestMapping("/guest_login")
    @CrossOrigin()
    @ResponseBody
    public String guest_login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String guestToken = KBZSDK.sendSmsCode(deviceId, phone);
            kbzUserImpl.updateOrInsertGuestUser(deviceId, phone, guestToken, "");
            return guestToken;
        } catch (Exception e) {
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //验证码登录
    @RequestMapping("/code_login")
    @CrossOrigin()
    @ResponseBody
    public String code_login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String code = json.getString("code");
            String guestToken = json.getString("guestToken");
            String idNo = json.getString("idNo");//身份信息
            String initiatorMSISDN = json.getString("initiatorMSISDN");
            if (initiatorMSISDN == null)
                initiatorMSISDN = phone;
            String token = KBZSDK.loginForSmsCode(deviceId, phone, code, guestToken, idNo, initiatorMSISDN); //第二部获取有用的token   这里登录完成需要把参数保存起来 以便后面刷新token
            kbzUserImpl.updateOrInsertGuestUser(deviceId, phone, guestToken, token);
            return token;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //查单
    @RequestMapping("/get_history")
    @CrossOrigin()
    @ResponseBody
    public String get_history(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String token = json.getString("token");
            long fromDate = json.getLongValue("fromDate");
            long toDate = json.getLongValue("toDate");
            int startNum = json.getIntValue("startNum");
            String get_history_ls = KBZSDK.getHistoryList(deviceId, phone, token, fromDate, toDate, startNum);  //获取历史订单列表
            return get_history_ls;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //查单明细
    @RequestMapping("/get_history_detail")
    @CrossOrigin()
    @ResponseBody
    public String get_history_detail(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String token = json.getString("token");
            String debitOrCredit = json.getString("debitOrCredit");
            if (debitOrCredit == null)
                debitOrCredit = "D";
            String transNo = json.getString("transNo");//订单号
            String get_detail = KBZSDK.getDetail(debitOrCredit, transNo, deviceId, phone, token); //获取转账详细
            return get_detail;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //转账发起
//    @RequestMapping("/kbz_transfer_order_generator")
//    @CrossOrigin()
//    @ResponseBody
//    public String kbz_transfer_order_generator(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
//        try {
//            JSONObject json = new JSONObject(cipherMap);
//            String deviceId = json.getString("deviceId");
//            String phone = json.getString("phone");
//            String token = json.getString("token");
//
//            String amount = json.getString("amount");
//            String note = json.getString("note");
//            String receiverMSISDN = json.getString("receiverMSISDN");
//            String pwd = json.getString("pwd");
//
////            String amount = "6";  //金额
////            String note = "test";  //描述
////            String receiverMSISDN = "09457748175";//对方账号(手机号码)
////            String pwd = "518218"; //密码
//            String transferToAccountResp = KBZSDK.transferToAccount(amount, note, receiverMSISDN, deviceId, phone, token);  //生成订单
//            JSONObject ret = new JSONObject();
//            ret.put("transferToAccountResp", transferToAccountResp);
//            return ret.toJSONString();
//        } catch (Exception e) {//请求异常处理
//            logger.info("http请求异常：" + e.getMessage());
//        }
//        return fail;
//    }

    //转账确认
    @RequestMapping("/kbz_transfer")
    @CrossOrigin()
    @ResponseBody
    public String kbz_transfer(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String token = json.getString("token");

            String amount = json.getString("amount");
            String note = json.getString("note");
            String receiverMSISDN = json.getString("receiverMSISDN");//对方账号(手机号码)
            String pwd = json.getString("pwd");

            String transferToAccountResp = KBZSDK.transferToAccount(amount, note, receiverMSISDN, deviceId, phone, token);  //生成订单
//            String transferToAccountResp = json.getString("transferToAccountResp");

            String transferResp = KBZSDK.transfer(transferToAccountResp, amount, note, receiverMSISDN, pwd, deviceId, phone, token); //确认转账
            return transferResp;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());

        }
        return fail;
    }

    //自动登录
    @RequestMapping("/kbz_auto_login")
    @CrossOrigin()
    @ResponseBody
    public String kbz_auto_login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String deviceId = json.getString("deviceId");
            String phone = json.getString("phone");
            String token = json.getString("token");
            return KBZSDK.autoLogin(deviceId, phone, token);
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }


}
